home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / applySpriteWizard.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  48.3 KB  |  1,130 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  ====================== applySpriteWizard ======================
  19. //
  20. //  SYNOPSIS
  21. //      Apply sprite hardware rendering to a particle shape.
  22. //
  23. //  INPUT ARGUMENTS
  24. //
  25. //      string $particleShape   The name of the particle shape. Must be the shape.
  26. //        string $imageName        The name of the base image
  27. //        int $startImage         The first image frame to use
  28. //        int $endImage            The end image frame to use
  29.  
  30. global proc int applySpriteWizard(
  31.                         string $particleShape,
  32.                         string $imageName,
  33.                         int $startImage, 
  34.                         int $endImage,
  35.                         int $animationOption, 
  36.                         int $imageSelection,
  37.                         int $cycle,
  38.                         float $cycleLength,
  39.                         int $invert )
  40. {
  41.     // Set sprite rendering attributes in the particle object
  42.     //
  43.     select -r $particleShape;
  44.     setAttr ($particleShape+".particleRenderType") 5;
  45.     setAttr ($particleShape+".depthSort") 1;
  46.  
  47.     //
  48.     // Use the existing sprite file texture, if it exists.
  49.     // Otherwise, create a new one.
  50.     //
  51.     string $fileTex = "";
  52.     $fileTex = particleSpriteFileTexture( $particleShape );
  53.     if( $fileTex == "" )
  54.     {
  55.         // Make a shader
  56.         //
  57.         string $lambert = `shadingNode -asShader lambert`;
  58.         string $lambertSG   = $lambert + "SG";
  59.         sets -renderable true -noSurfaceShader true -empty -name $lambertSG;
  60.         connectAttr -f ($lambert+".outColor") ($lambertSG+".surfaceShader");
  61.  
  62.         $fileTex = `shadingNode -asTexture file`;
  63.         string $placeTex = `shadingNode -asUtility place2dTexture`;
  64.         connectAttr -f ($placeTex+".coverage") ($fileTex+".coverage");
  65.         connectAttr -f ($placeTex+".translateFrame")
  66.             ($fileTex+".translateFrame");
  67.         connectAttr -f ($placeTex+".rotateFrame") ($fileTex+".rotateFrame");
  68.         connectAttr -f ($placeTex+".mirrorU") ($fileTex+".mirrorU");
  69.         connectAttr -f ($placeTex+".mirrorV") ($fileTex+".mirrorV");
  70.         connectAttr -f ($placeTex+".stagger") ($fileTex+".stagger");
  71.         connectAttr -f ($placeTex+".wrapU") ($fileTex+".wrapU");
  72.         connectAttr -f ($placeTex+".wrapV") ($fileTex+".wrapV");
  73.         connectAttr -f ($placeTex+".repeatUV") ($fileTex+".repeatUV");
  74.         connectAttr -f ($placeTex+".offset") ($fileTex+".offset");
  75.         connectAttr -f ($placeTex+".rotateUV") ($fileTex+".rotateUV");
  76.         connectAttr -f ($placeTex+".noiseUV") ($fileTex+".noiseUV");
  77.         connectAttr -f ($placeTex+".vertexUvOne") ($fileTex+".vertexUvOne");
  78.         connectAttr -f ($placeTex+".vertexUvTwo") ($fileTex+".vertexUvTwo");
  79.         connectAttr -f ($placeTex+".vertexUvThree") ($fileTex+".vertexUvThree");
  80.         connectAttr -f ($placeTex+".vertexCameraOne") ($fileTex+".vertexCameraOne");
  81.         connectAttr ($placeTex+".outUV") ($fileTex+".uv");
  82.         connectAttr ($placeTex+".outUvFilterSize")
  83.             ($fileTex+".uvFilterSize");
  84.  
  85.         // Hook the file texture to the color. Also automatically
  86.         // hook up transparency.
  87.         //
  88.         connectAttr -f ($fileTex+".outColor") ($lambert+".color");
  89.         connectAttr -f ($fileTex+".outTransparency") ($lambert+".transparency");
  90.  
  91.         // Assign the shader.
  92.         //
  93.         sets -e -forceElement $lambertSG $particleShape;
  94.     }
  95.  
  96.     // Assign the source image.
  97.     // We use whatever is the default path.
  98.     // Enable frame extension and hardware texture cycling.
  99.     //
  100.     string $currentFileName = `getAttr ($fileTex+".fileTextureName")`;
  101.     int $currentUSE = `getAttr ($fileTex+".useFrameExtension")`;
  102.     int $currentUHTC = `getAttr ($fileTex+".useHardwareTextureCycling")`;
  103.     int $currentSCE = `getAttr ($fileTex+".startCycleExtension")`;
  104.     int $currentECE = `getAttr ($fileTex+".endCycleExtension")`;
  105.  
  106.     if( $currentFileName != $imageName )
  107.     {
  108.         setAttr -type "string" ($fileTex+".fileTextureName") $imageName;
  109.     }
  110.     if( $currentUSE != 1 )
  111.     {
  112.         setAttr ($fileTex+".useFrameExtension") 1;
  113.     }
  114.     if( $currentUHTC != 1 )
  115.     {
  116.         setAttr ($fileTex+".useHardwareTextureCycling") 1;
  117.     }
  118.     if( $currentSCE != $startImage )
  119.     {
  120.         setAttr ($fileTex+".startCycleExtension") $startImage;
  121.     }
  122.     if( $currentECE != $endImage )
  123.     {
  124.         setAttr ($fileTex+".endCycleExtension")   $endImage;
  125.     }
  126.  
  127.     //
  128.     // If there is only one image in the sequence, turn OFF useFrameExtension
  129.     // and useHardwareTextureCycling.
  130.     //
  131.     if( $startImage == $endImage )
  132.     {
  133.         setAttr ($fileTex+".useFrameExtension") 0;
  134.         setAttr ($fileTex+".useHardwareTextureCycling") 0;
  135.     }
  136.  
  137.     // Set expression on frameExtension to force file load and reset random generator
  138.     //
  139.     string $frameExtensionDrivers[] =
  140.         `listConnections -plugs false -source true -destination false
  141.         ($fileTex+".frameExtension")`;
  142.  
  143.     if( size( $frameExtensionDrivers ) > 0 )
  144.     {
  145.         string $frameExtensionExpr[] =
  146.             `ls -type expression $frameExtensionDrivers`;
  147.  
  148.         if( size( $frameExtensionExpr ) > 0 )
  149.         {
  150.             delete $frameExtensionExpr;
  151.         }
  152.  
  153.         $frameExtensionDrivers =
  154.             `listConnections -shapes true -plugs true
  155.             -source true -destination false
  156.             ($fileTex+".frameExtension")`;
  157.         if( size( $frameExtensionDrivers ) > 0 )
  158.         {
  159.             disonnectAttr
  160.                 $frameExtensionDrivers[0] ($fileTex+".frameExtension");
  161.         }
  162.     }
  163.  
  164.     expression -s "if (frame <= 1) seed(1);\n\nint $start = startCycleExtension;\nint $end =   endCycleExtension;\nif (frame <= $end-$start+1)\n\tframeExtension = $start + frame - 1;\nelse\n\tframeExtension = $end;"  -o $fileTex -ae 1 -uc all ;
  165.  
  166.     //
  167.     // Add the needed attributes and set up the
  168.     // expressions.
  169.     //
  170.     addAttributesForExpressions( $particleShape );
  171.     setAttr ($particleShape+".SpriteAnimation") $animationOption;
  172.  
  173.     // If particles live forever, the animation option won't work.
  174.     // Switch them to "constant" to that the user can see their animation.
  175.     //
  176.     int $lifespanMode = `getAttr ($particleShape+".lifespanMode")`;
  177.     if (0 == $lifespanMode) {
  178.         setAttr ($particleShape+".lifespanMode") 1;
  179.     }
  180.  
  181.     setAttr ($particleShape+".SpriteStartOption") $imageSelection;
  182.     setAttr ($particleShape+".SpriteCyclePattern") $cycle;
  183.     setAttr ($particleShape+".SpriteCycleLength") $cycleLength;
  184.     setAttr ($particleShape+".SpriteInvertCycle") $invert;
  185.  
  186.     string $expressions[] =
  187.         createSpriteExpressions( $particleShape, $fileTex );
  188.  
  189.     dynExpression -r -s $expressions[0] $particleShape;
  190.     dynExpression -c -s $expressions[1] $particleShape;
  191.  
  192.     print("// Setting up particle sprite for "+$particleShape+"\n");
  193.     print("// image: "+$imageName+"\n");
  194.     print("// Sprite Image Start: "+$startImage+", end: "+$endImage+"\n");
  195.  
  196.     select `listRelatives -parent $particleShape`;
  197.     return 0;
  198. }
  199.  
  200. global proc addAttributesForExpressions( string $particleShape )
  201. {
  202.     //
  203.     // First, we will add the scalar attributes.
  204.     //
  205.     if( `attributeQuery -exists -node $particleShape "spriteTwist"` == 0 )
  206.     {
  207.         addAttr -k 1 -is true -ln "spriteTwist" 
  208.             -at "float" -min -180 -max 180 -dv 0.0 
  209.             $particleShape;
  210.     }
  211.     if( `attributeQuery -exists -node $particleShape "spriteScaleX"` == 0 )
  212.     {
  213.         addAttr -k 1 -is true -ln "spriteScaleX" -dv 1.0 $particleShape;
  214.     }
  215.     if( `attributeQuery -exists -node $particleShape "spriteScaleY"` == 0 )
  216.     {
  217.         addAttr -k 1 -is true -ln "spriteScaleY" -dv 1.0 $particleShape;
  218.     }
  219.     if( `attributeQuery -exists -node $particleShape "spriteNum"` == 0 )
  220.     {
  221.         addAttr -k 1 -is true -ln "spriteNum" -at long -dv 1 $particleShape;
  222.     }
  223.     if( `attributeQuery -exists -node $particleShape "useLighting"` == 0 )
  224.     {
  225.         addAttr -k 1 -is true -ln "useLighting" -at bool -dv false $particleShape;
  226.     }
  227.  
  228.  
  229.  
  230.     //
  231.     // The "ShowSpriteHelp" attribute is a special attribute.
  232.     // It is hooked up to a scriptJob that will display a
  233.     // help window whenever this attribute is changed.
  234.     //
  235. //    if( `attributeQuery -exists -node $particleShape ShowSpriteHelp` == 0 )
  236. //    {
  237. //        addAttr -ln "ShowSpriteHelp" -at short -min 0 -max 0 -dv 0 $particleShape;
  238. //    }
  239.     // scriptJob -attributeChange ($particleShape+".ShowSpriteHelp") "showSpriteHelp";
  240.     // addSpriteHelpScriptNode();
  241.  
  242.     //
  243.     // The "__ApplySprite__" attribute marks this particle object
  244.     // as having this script run on it, setting up some smart
  245.     // presets for particle sprite animation.  It is used by the
  246.     // sprite wizard to identify particles that have already
  247.     // been set up by the wizard.
  248.     //
  249.     if( `attributeQuery -exists -node $particleShape "__ApplySprite__"` == 0 )
  250.     {
  251.         addAttr -ln "__ApplySprite__" -at message $particleShape;
  252.     }
  253.  
  254.     //
  255.     // The "SpriteAnimation" attribute controls whether or
  256.     // not the sprites used for each particle will change over
  257.     // the lifespan of the particles.  A value of 0 means that
  258.     // whatever sprite is chosen for each particle at the time
  259.     // of its birth will be the sprite used for the entire life
  260.     // of the particles.
  261.     //
  262.     if( `attributeQuery -exists -node $particleShape SpriteAnimation` == 0 ) {
  263.         addAttr -k 1 -ln "SpriteAnimation" -at enum -enumName "off:on" -dv 0 $particleShape;
  264.     }
  265.     else
  266.     {
  267.         if( ( `getAttr -type ($particleShape+".SpriteAnimation")` != "short" ) &&
  268.             ( `getAttr -type ($particleShape+".SpriteAnimation")` != "long" ) &&
  269.             ( `getAttr -type ($particleShape+".SpriteAnimation")` != "enum" ) &&
  270.             ( `getAttr -type ($particleShape+".SpriteAnimation")` != "float" ) &&
  271.             ( `getAttr -type ($particleShape+".SpriteAnimation")` != "double" ) )
  272.         {
  273.             warning("Attribute \"SpriteAnimation\" already exists and is not a numeric type.  Cannot proceed.");
  274.         }
  275.     }
  276.  
  277.     //
  278.     // The "SpriteStartOption" attribute tells the expressions
  279.     // how to determine the initial sprite to use for each
  280.     // particles.  The values are as follows:
  281.     //
  282.     //     0:  Use the first sprite in the sequence.
  283.     //     1:  Use the particle's "particleId" value as the starting
  284.     //         index.  This value will be "wrapped" to be a value
  285.     //         within the valid range of indices.
  286.     //     2:  Choose a random index from the range of valid indices.
  287.     //     3:  Use the value of the "startIndexPP" per-particle
  288.     //         attribute.  It is then the user's responsibility to
  289.     //         set this value sometime BEFORE the creation expression
  290.     //         gets called.  This could either be put at the top of
  291.     //         the creation expression or use a ramp.
  292.     //     4:  Use the value of the "SpriteNumRamp" per-particle
  293.     //         attribute.  This attribute will be set up to use
  294.     //         a ramp by this script.  The user can then edit the
  295.     //         ramp to control the animation of the "SpriteNumPP"
  296.     //         attribute.
  297.     //
  298.     if( `attributeQuery -exists -node $particleShape SpriteStartOption` == 0 )
  299.     {
  300.         addAttr -k 1 -ln "SpriteStartOption" -at enum
  301.                 -enumName "first:particleId:random:custom:ramp" -dv 0 $particleShape;
  302.     }
  303.     else
  304.     {
  305.         if( ( `getAttr -type ($particleShape+".SpriteStartOption")` != "short" ) &&
  306.             ( `getAttr -type ($particleShape+".SpriteStartOption")` != "long" ) &&
  307.             ( `getAttr -type ($particleShape+".SpriteStartOption")` != "enum" ) &&
  308.             ( `getAttr -type ($particleShape+".SpriteStartOption")` != "float" ) &&
  309.             ( `getAttr -type ($particleShape+".SpriteStartOption")` != "double" ) )
  310.         {
  311.             warning("Attribute \"SpriteStartOption\" already exists and is not a numeric type.  Can not proceed.");
  312.         }
  313.     }
  314.  
  315.     //
  316.     // The "SpriteCyclePattern" attribute controls how the sprites
  317.     // animate over the lifespan of the particles.  This is only used
  318.     // if the "SpriteAnimation" attribute is set to 1.  The values
  319.     // are as follows:
  320.     //
  321.     //     0:  Linearly increasing sprite index.
  322.     //
  323.     //        1--|     /
  324.     //           |    /
  325.     //           |   /
  326.     //           |  /
  327.     //           | /
  328.     //        0--|/_______________________
  329.     //        
  330.     //     1:  Ease-In/Ease-Out increasing sprite index.
  331.     //                               _____
  332.     //        1--|                ___/
  333.     //           |              _/
  334.     //           |             / 
  335.     //           |            |
  336.     //           |          _/
  337.     //           |      ___/
  338.     //        0--|_____/______________________
  339.     //        
  340.     //     2:  Linear increasing followed by linear decrease.
  341.     //
  342.     //        1--|     /\
  343.     //           |    /  \
  344.     //           |   /    \
  345.     //           |  /      \
  346.     //           | /        \
  347.     //        0--|/__________\____________
  348.     //        
  349.     //     3:  Ease-In/Ease-Out increase and decrease.
  350.     //                             ________
  351.     //        1--|              __/        \__
  352.     //           |            _/              \_
  353.     //           |           /                  \
  354.     //           |          |                    |
  355.     //           |        _/                      \_
  356.     //           |     __/                          \__
  357.     //        0--|____/                                \____
  358.     //        
  359.     // This is relative to the starting index for each particle.
  360.     // If a particle started with an index half way through the
  361.     // range of sprites and the "SpriteCyclePattern" attribute is
  362.     // 0, then the animation on the sprite would look like this:
  363.     //
  364.     //        1--|  /
  365.     //           | /
  366.     //           |/
  367.     //           |     /
  368.     //           |    /
  369.     //        0--|___/____________________
  370.     //        
  371.     if( `attributeQuery -exists -node $particleShape SpriteCyclePattern` == 0 )
  372.     {
  373.         addAttr -k 1-ln "SpriteCyclePattern" -at enum
  374.             -enumName "linearUp:easeUp:linearUpDown:easeUpDown:custom:ramp" -dv 0 $particleShape;
  375.     }
  376.     else
  377.     {
  378.         if( ( `getAttr -type ($particleShape+".SpriteCyclePattern")` != "short" ) &&
  379.             ( `getAttr -type ($particleShape+".SpriteCyclePattern")` != "long" ) &&
  380.             ( `getAttr -type ($particleShape+".SpriteCyclePattern")` != "enum" ) &&
  381.             ( `getAttr -type ($particleShape+".SpriteCyclePattern")` != "float" ) &&
  382.             ( `getAttr -type ($particleShape+".SpriteCyclePattern")` != "double" ) )
  383.         {
  384.             warning("Attribute \"SpriteCyclePattern\" already exists and is not a numeric type.  Can not proceed.");
  385.         }
  386.     }
  387.  
  388.     //
  389.     // The "SpriteCycleLength" attribute controls how fast the
  390.     // sprite animation occurs for each particle.  A value of
  391.     // 0 will make the cycle happen once over the lifespan of
  392.     // each particle.  Any value greater than 0 will make the
  393.     // cycle happen once in that many frames and repeat until
  394.     // the particle dies.  For example, if a particle's lifespan
  395.     // was 60 frames, and the "SpriteCycleLength" was 30, then
  396.     // the cycle, as defined by the "SpriteCyclePattern" attribute,
  397.     // would happen twice over the particle's lifespan, like this:
  398.     //
  399.     //        1--|     /     /
  400.     //           |    /     /
  401.     //           |   /     /
  402.     //           |  /     /
  403.     //           | /     /
  404.     //        0--|/_____/_________________
  405.     //        
  406.     if( `attributeQuery -exists -node $particleShape SpriteCycleLength` == 0 )
  407.     {
  408.         addAttr -k 1 -ln "SpriteCycleLength" -at double -min 0 -dv 0 $particleShape;
  409.     }
  410.     else
  411.     {
  412.         if( ( `getAttr -type ($particleShape+".SpriteCycleLength")` != "short" ) &&
  413.             ( `getAttr -type ($particleShape+".SpriteCycleLength")` != "long" ) &&
  414.             ( `getAttr -type ($particleShape+".SpriteCycleLength")` != "float" ) &&
  415.             ( `getAttr -type ($particleShape+".SpriteCycleLength")` != "double" ) )
  416.         {
  417.             warning("Attribute \"SpriteCycleLength\" already exists and is not a numeric type.  Can not proceed.");
  418.         }
  419.     }
  420.  
  421.     //
  422.     // The "SpriteInvertCycle" attribute tells the expressions
  423.     // whether to invert the animation "SpriteCyclePattern"
  424.     // attribute.  For instance, if the "SpriteCyclePattern"
  425.     // attribute is 3 and the "SpriteInvertCycle" attribute is
  426.     // TRUE, then the resulting cycle shape would be:
  427.     //            ____                                  ____
  428.     //        1--|    \__                            __/
  429.     //           |       \_                        _/
  430.     //           |         \                      /
  431.     //           |          |                    |
  432.     //           |           \_                _/
  433.     //           |             \__          __/
  434.     //        0--|                \________/
  435.     //
  436.     if( `attributeQuery -exists -node $particleShape SpriteInvertCycle` == 0 )
  437.     {
  438.         addAttr -ln "SpriteInvertCycle" -at bool -dv false $particleShape;
  439.     }
  440.     else
  441.     {
  442.         if( ( `getAttr -type ($particleShape+".SpriteInvertCycle")` != "short" ) &&
  443.             ( `getAttr -type ($particleShape+".SpriteInvertCycle")` != "long" ) &&
  444.             ( `getAttr -type ($particleShape+".SpriteInvertCycle")` != "float" ) &&
  445.             ( `getAttr -type ($particleShape+".SpriteInvertCycle")` != "double" ) &&
  446.             ( `getAttr -type ($particleShape+".SpriteInvertCycle")` != "bool" ) )
  447.         {
  448.             warning("Attribute \"SpriteInvertCycle\" already exists and is not a numeric or boolean type.  Can not proceed.");
  449.         }
  450.     }
  451.  
  452.     //
  453.     // Now we add the per-particle attributes.
  454.     //
  455.  
  456.     //
  457.     // The "ageNormalized" attribute automatically gets computed
  458.     // by the particle shape to be the ratio of each particle's
  459.     // current age to its lifespan.
  460.     //
  461.     if( `attributeQuery -exists -node $particleShape ageNormalized` == 0 )
  462.     {
  463.         addAttr -ln ageNormalized -dt doubleArray $particleShape;
  464.     }
  465.     else
  466.     {
  467.         if( `getAttr -type ($particleShape+".ageNormalized")`
  468.             != "doubleArray" )
  469.         {
  470.             warning("Attribute \"ageNormalized\" already exists and is not a doubleArray type.  Can not proceed.");
  471.         }
  472.     }
  473.     if( `attributeQuery -exists -node $particleShape ageNormalized0` == 0 )
  474.     {
  475.         addAttr -ln ageNormalized0 -dt doubleArray $particleShape;
  476.     }
  477.     else
  478.     {
  479.         if( `getAttr -type ($particleShape+".ageNormalized0")`
  480.             != "doubleArray" )
  481.         {
  482.             warning("Attribute \"ageNormalized0\" already exists and is not a doubleArray type.  Can not proceed.");
  483.         }
  484.     }
  485.  
  486.     //
  487.     // The "spriteNumPP" attribute is the attribute that
  488.     // tells the hardware rendering which sprite to use.
  489.     // This is 1-based, meaning that a value of 1 will mean
  490.     // to use the first sprite in the sprite range.
  491.     //
  492.     if( `attributeQuery -exists -node $particleShape spriteNumPP` == 0 )
  493.     {
  494.         addAttr -ln spriteNumPP -dt doubleArray $particleShape;
  495.     }
  496.     else
  497.     {
  498.         if( `getAttr -type ($particleShape+".spriteNumPP")`
  499.             != "doubleArray" )
  500.         {
  501.             warning("Attribute \"spriteNumPP\" already exists and is not a doubleArray type.  Can not proceed.");
  502.         }
  503.     }
  504.     if( `attributeQuery -exists -node $particleShape spriteNumPP0` == 0 )
  505.     {
  506.         addAttr -ln spriteNumPP0 -dt doubleArray $particleShape;
  507.     }
  508.     else
  509.     {
  510.         if( `getAttr -type ($particleShape+".spriteNumPP0")`
  511.             != "doubleArray" )
  512.         {
  513.             warning("Attribute \"spriteNumPP0\" already exists and is not a doubleArray type.  Can not proceed.");
  514.         }
  515.     }
  516.  
  517.     //
  518.     // The "startIndexPP" attribute is used to determine the
  519.     // sprite that each particle gets when it is created.  It
  520.     // is then used in the runtime expression to control the
  521.     // shape of the animation.
  522.     //
  523.     if( `attributeQuery -exists -node $particleShape startIndexPP` == 0 )
  524.     {
  525.         addAttr -ln startIndexPP -dt doubleArray $particleShape;
  526.     }
  527.     else
  528.     {
  529.         if( `getAttr -type ($particleShape+".startIndexPP")`
  530.             != "doubleArray" )
  531.         {
  532.             warning("Attribute \"startIndexPP\" already exists and is not a doubleArray type.  Can not proceed.");
  533.         }
  534.     }
  535.     if( `attributeQuery -exists -node $particleShape startIndexPP0` == 0 )
  536.     {
  537.         addAttr -ln startIndexPP0 -dt doubleArray $particleShape;
  538.     }
  539.     else
  540.     {
  541.         if( `getAttr -type ($particleShape+".startIndexPP0")`
  542.             != "doubleArray" )
  543.         {
  544.             warning("Attribute \"startIndexPP0\" already exists and is not a doubleArray type.  Can not proceed.");
  545.         }
  546.     }
  547.  
  548.     //
  549.     // The "spriteNumRamp" attribute is used if the user
  550.     // chooses to control the sprite animation using a ramp
  551.     // a default ramp will be set up to drive this attribute.
  552.     //
  553.     if( `attributeQuery -exists -node $particleShape spriteNumRamp` == 0 )
  554.     {
  555.         addAttr -ln spriteNumRamp -dt doubleArray $particleShape;
  556.     }
  557.     else
  558.     {
  559.         if( `getAttr -type ($particleShape+".spriteNumRamp")`
  560.             != "doubleArray" )
  561.         {
  562.             warning("Attribute \"spriteNumRamp\" already exists and is not a doubleArray type.  Can not proceed.");
  563.         }
  564.     }
  565.     if( `attributeQuery -exists -node $particleShape spriteNumRamp0` == 0 )
  566.     {
  567.         addAttr -ln spriteNumRamp0 -dt doubleArray $particleShape;
  568.     }
  569.     else
  570.     {
  571.         if( `getAttr -type ($particleShape+".spriteNumRamp0")`
  572.             != "doubleArray" )
  573.         {
  574.             warning("Attribute \"spriteNumRamp0\" already exists and is not a doubleArray type.  Can not proceed.");
  575.         }
  576.     }
  577.  
  578.     //
  579.     // The "spriteNumRampU" attribute is added to allow the user to
  580.     // decide the U-position in the ramp that drives the "spriteNumRamp"
  581.     // attribute to get the value for "spriteNumRamp".  No animation
  582.     // is put into this attribute by default.  The V-position of this
  583.     // ramp is always based on the particles' age and lifespan, so
  584.     // that the entire ramp will be used exactly once over each
  585.     // particle's lifespan.
  586.     //
  587.     if( `attributeQuery -exists -node $particleShape spriteNumRampU` == 0 )
  588.     {
  589.         addAttr -ln spriteNumRampU -dt doubleArray $particleShape;
  590.     }
  591.     else
  592.     {
  593.         if( `getAttr -type ($particleShape+".spriteNumRampU")`
  594.             != "doubleArray" )
  595.         {
  596.             warning("Attribute \"spriteNumRampU\" already exists and is not a doubleArray type.  Can not proceed.");
  597.         }
  598.     }
  599.     if( `attributeQuery -exists -node $particleShape spriteNumRampU0` == 0 )
  600.     {
  601.         addAttr -ln spriteNumRampU0 -dt doubleArray $particleShape;
  602.     }
  603.     else
  604.     {
  605.         if( `getAttr -type ($particleShape+".spriteNumRampU0")`
  606.             != "doubleArray" )
  607.         {
  608.             warning("Attribute \"spriteNumRampU0\" already exists and is not a doubleArray type.  Can not proceed.");
  609.         }
  610.     }
  611.  
  612.     //
  613.     // If the "spriteNumRamp" attribute is not already driven
  614.     // by a a ramp, then create a ramp to drive it.
  615.     //
  616.     int $needNewRamp = 1;
  617.     string $drivingRamp[] = `listConnections -source true -destination false
  618.         -plugs false -shapes true ($particleShape+".spriteNumRamp")`;
  619.     if( size( $drivingRamp ) > 0 )
  620.     {
  621.         string $ramps[] = `ls -type arrayMapper $drivingRamp`;
  622.         if( size( $ramps ) == 0 )
  623.         {
  624.             disconnectAttr
  625.                 $drivingRamp[0] ($particleShape+"spriteNumRamp");
  626.         }
  627.         else
  628.         {
  629.             $needNewRamp = 0;
  630.         }
  631.     }
  632.     if( $needNewRamp == 1 )
  633.     {
  634.         //
  635.         // Now attach a new ramp to the "spriteNumRamp" attribute, using
  636.         // the "spriteNumRampU" attribute to drive the U-position in the
  637.         // ramp.
  638.         //
  639.         string $spriteNumRamp = `shadingNode -asTexture ramp`;
  640.         $spriteNumRamp = `rename $spriteNumRamp "spriteNumRamp#"`;
  641.         removeMultiInstance -break true ($spriteNumRamp+".colorEntryList[2]");
  642.         setAttr ($spriteNumRamp+".colorEntryList[0].position") 0;
  643.         setAttr ($spriteNumRamp+".colorEntryList[1].position") 1;
  644.         setAttr ($spriteNumRamp+".colorEntryList[0].color") -type double3 0 0 0;
  645.         setAttr ($spriteNumRamp+".colorEntryList[1].color") -type double3 1 1 1;
  646.  
  647.         string $arrayMappers[] = `arrayMapper -target $particleShape
  648.             -destAttr spriteNumRamp -inputU spriteNumRampU
  649.             -inputV ageNormalized -mapTo $spriteNumRamp`;
  650.         rename $arrayMappers[0] "spriteNumRampMapper#";
  651.     }
  652. }
  653.  
  654. global proc string[] createSpriteExpressions( string $particleShape, string $fileTexture )
  655. //
  656. // Add the expressions needed to control the sprites
  657. // to the particle shape.  Any expressions that were there
  658. // before are not deleted.  The new expressions are
  659. // appended to the string AFTER the existing expressions.
  660. // Any expressions that were added to this particle
  661. // shape from previous calls to applySprite() will be
  662. // removed and replaced.
  663. //
  664. {
  665.     string $runtimeExpression = `dynExpression -q -r -s $particleShape`;
  666.     string $creationExpression = `dynExpression -q -c -s $particleShape`;
  667.  
  668.     string $creationExpressionToInsert = "";
  669.     string $runtimeExpressionToInsert = "";
  670.  
  671.     $creationExpressionToInsert += "// __ApplySprite_Start_Tag__\n";
  672.     $creationExpressionToInsert += "//\n";
  673.     $creationExpressionToInsert += "// Do not delete the tag above.  It is used to properly\n";
  674.     $creationExpressionToInsert += "// replace this expression if the applySprite() script\n";
  675.     $creationExpressionToInsert += "// is executed again.\n";
  676.     $creationExpressionToInsert += "//\n";
  677.     $creationExpressionToInsert += "int $firstImageNumber = "+$fileTexture+".startCycleExtension;\n";
  678.     $creationExpressionToInsert += "int $lastImageNumber = "+$fileTexture+".endCycleExtension;\n";
  679.     $creationExpressionToInsert += "int $imageCount = $lastImageNumber - $firstImageNumber + 1;\n";
  680.     $creationExpressionToInsert += "int $one_based_start = 1;\n";
  681.     $creationExpressionToInsert += "\n";
  682.     $creationExpressionToInsert += "if( SpriteAnimation == 0 )\n";
  683.     $creationExpressionToInsert += "//\n";
  684.     $creationExpressionToInsert += "// Fixed SpriteNumPP\n";
  685.     $creationExpressionToInsert += "//\n";
  686.     $creationExpressionToInsert += "{\n";
  687.     $creationExpressionToInsert += "\t//\n";
  688.     $creationExpressionToInsert += "\t// Use the first image in the sequence\n";
  689.     $creationExpressionToInsert += "\t//\n";
  690.     $creationExpressionToInsert += "\tif( SpriteStartOption == 0 )\n";
  691.     $creationExpressionToInsert += "\t{\n";
  692.     $creationExpressionToInsert += "\t\tspriteNumPP = $one_based_start;\n";
  693.     $creationExpressionToInsert += "\t}\n";
  694.     $creationExpressionToInsert += "\t//\n";
  695.     $creationExpressionToInsert += "\t// Use the particle's ID value ( MOD imageCount )\n";
  696.     $creationExpressionToInsert += "\t//\n";
  697.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 1 )\n";
  698.     $creationExpressionToInsert += "\t{\n";
  699.     $creationExpressionToInsert += "\t\tspriteNumPP = $one_based_start + ( particleId % $imageCount );\n";
  700.     $creationExpressionToInsert += "\t}\n";
  701.     $creationExpressionToInsert += "\t//\n";
  702.     $creationExpressionToInsert += "\t// Random\n";
  703.     $creationExpressionToInsert += "\t//\n";
  704.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 2 )\n";
  705.     $creationExpressionToInsert += "\t{\n";
  706.     $creationExpressionToInsert += "\t\tint $s = rand( $imageCount + 0.99 ) + $one_based_start;\n";
  707.     $creationExpressionToInsert += "\t\tspriteNumPP = $s;\n";
  708.     $creationExpressionToInsert += "\t}\n";
  709.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 3 )\n";
  710.     $creationExpressionToInsert += "\t//\n";
  711.     $creationExpressionToInsert += "\t// Custom Start: If your sprites are not animated\n";
  712.     $creationExpressionToInsert += "\t//\n";
  713.     $creationExpressionToInsert += "\t{\n";
  714.     $creationExpressionToInsert += "\t\t//\n";
  715.     $creationExpressionToInsert += "\t\t// Put your own expressions to set spriteNumPP here.\n";
  716.     $creationExpressionToInsert += "\t\t//\n";
  717.     $creationExpressionToInsert += "\t\tspriteNumPP = 1;\n";
  718.     $creationExpressionToInsert += "\t}\n";
  719.     $creationExpressionToInsert += "\t//\n";
  720.     $creationExpressionToInsert += "\t// Ramp, spriteNumRamp\n";
  721.     $creationExpressionToInsert += "\t//\n";
  722.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 4 )\n";
  723.     $creationExpressionToInsert += "\t{\n";
  724.     $creationExpressionToInsert += "\t\tfloat $rampValue = spriteNumRamp;\n";
  725.     $creationExpressionToInsert += "\t\tif( SpriteInvertCycle == 1 )\n";
  726.     $creationExpressionToInsert += "\t\t{\n";
  727.     $creationExpressionToInsert += "\t\t\t$rampValue = 1.0 - $rampValue;\n";
  728.     $creationExpressionToInsert += "\t\t\twhile( $rampValue < 0 )\n";
  729.     $creationExpressionToInsert += "\t\t\t{\n";
  730.     $creationExpressionToInsert += "\t\t\t\t$rampValue = $rampValue + 1.0;\n";
  731.     $creationExpressionToInsert += "\t\t\t}\n";
  732.     $creationExpressionToInsert += "\t\t\tif( $rampValue > 1.0 )\n";
  733.     $creationExpressionToInsert += "\t\t\t{\n";
  734.     $creationExpressionToInsert += "\t\t\t\t$rampValue = $rampValue % 1.0;\n";
  735.     $creationExpressionToInsert += "\t\t\t}\n";
  736.     $creationExpressionToInsert += "\t\t}\n";
  737.     $creationExpressionToInsert += "\t\tint $s = $one_based_start + ( $rampValue * ( $imageCount - 0.01 ) );\n";
  738.     $creationExpressionToInsert += "\t\tspriteNumPP = $s;\n";
  739.     $creationExpressionToInsert += "\t}\n";
  740.     $creationExpressionToInsert += "}\n";
  741.     $creationExpressionToInsert += "else if( SpriteAnimation == 1 )\n";
  742.     $creationExpressionToInsert += "//\n";
  743.     $creationExpressionToInsert += "// Cycle SpriteNumPP\n";
  744.     $creationExpressionToInsert += "//\n";
  745.     $creationExpressionToInsert += "{\n";
  746.     $creationExpressionToInsert += "\t//\n";
  747.     $creationExpressionToInsert += "\t// Use the first\/last image in the sequence\n";
  748.     $creationExpressionToInsert += "\t//\n";
  749.     $creationExpressionToInsert += "\tif( SpriteStartOption == 0 )\n";
  750.     $creationExpressionToInsert += "\t{\n";
  751.     $creationExpressionToInsert += "\t\tif( SpriteInvertCycle == 1 )\n";
  752.     $creationExpressionToInsert += "\t\t{\n";
  753.     $creationExpressionToInsert += "\t\t\tspriteNumPP = $imageCount;\n";
  754.     $creationExpressionToInsert += "\t\t}\n";
  755.     $creationExpressionToInsert += "\t\telse\n";
  756.     $creationExpressionToInsert += "\t\t{\n";
  757.     $creationExpressionToInsert += "\t\t\tspriteNumPP = $one_based_start;\n";
  758.     $creationExpressionToInsert += "\t\t}\n";
  759.     $creationExpressionToInsert += "\t}\n";
  760.     $creationExpressionToInsert += "\t//\n";
  761.     $creationExpressionToInsert += "\t// Use the particle's ID value ( MOD imageCount )\n";
  762.     $creationExpressionToInsert += "\t//\n";
  763.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 1 )\n";
  764.     $creationExpressionToInsert += "\t{\n";
  765.     $creationExpressionToInsert += "\t\tspriteNumPP = $one_based_start + ( particleId % $imageCount );\n";
  766.     $creationExpressionToInsert += "\t}\n";
  767.     $creationExpressionToInsert += "\t//\n";
  768.     $creationExpressionToInsert += "\t// Random\n";
  769.     $creationExpressionToInsert += "\t//\n";
  770.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 2 )\n";
  771.     $creationExpressionToInsert += "\t{\n";
  772.     $creationExpressionToInsert += "\t\tint $s = rand( $imageCount - 0.01 ) + $one_based_start;\n";
  773.     $creationExpressionToInsert += "\t\tspriteNumPP = $s;\n";
  774.     $creationExpressionToInsert += "\t}\n";
  775.     $creationExpressionToInsert += "\t//\n";
  776.     $creationExpressionToInsert += "\t// Custom Start: if your sprites are animated\n";
  777.     $creationExpressionToInsert += "\t//\n";
  778.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 3 )\n";
  779.     $creationExpressionToInsert += "\t{\n";
  780.     $creationExpressionToInsert += "\t\t//\n";
  781.     $creationExpressionToInsert += "\t\t// Put your own expressions to set spriteNumPP here .\n";
  782.     $creationExpressionToInsert += "\t\t//\n";
  783.     $creationExpressionToInsert += "\t\tspriteNumPP = 1;\n";
  784.     $creationExpressionToInsert += "\t}\n";
  785.     $creationExpressionToInsert += "\t//\n";
  786.     $creationExpressionToInsert += "\t// Ramp, spriteNumRamp\n";
  787.     $creationExpressionToInsert += "\t//\n";
  788.     $creationExpressionToInsert += "\telse if( SpriteStartOption == 4 )\n";
  789.     $creationExpressionToInsert += "\t{\n";
  790.     $creationExpressionToInsert += "\t\tfloat $rampValue = spriteNumRamp;\n";
  791.     $creationExpressionToInsert += "\t\tif( SpriteInvertCycle == 1 )\n";
  792.     $creationExpressionToInsert += "\t\t{\n";
  793.     $creationExpressionToInsert += "\t\t\t$rampValue = 1.0 - $rampValue;\n";
  794.     $creationExpressionToInsert += "\t\t\twhile( $rampValue < 0 )\n";
  795.     $creationExpressionToInsert += "\t\t\t{\n";
  796.     $creationExpressionToInsert += "\t\t\t\t$rampValue = $rampValue + 1.0;\n";
  797.     $creationExpressionToInsert += "\t\t\t}\n";
  798.     $creationExpressionToInsert += "\t\t\tif( $rampValue > 1.0 )\n";
  799.     $creationExpressionToInsert += "\t\t\t{\n";
  800.     $creationExpressionToInsert += "\t\t\t\t$rampValue = $rampValue % 1.0;\n";
  801.     $creationExpressionToInsert += "\t\t\t}\n";
  802.     $creationExpressionToInsert += "\t\t}\n";
  803.     $creationExpressionToInsert += "\t\tint $s = $one_based_start + ( $rampValue * ( $imageCount - 0.01 ) );\n";
  804.     $creationExpressionToInsert += "\t\tspriteNumPP = $s;\n";
  805.     $creationExpressionToInsert += "\t}\n";
  806.     $creationExpressionToInsert += "}\n";
  807.     $creationExpressionToInsert += "\n";
  808.     $creationExpressionToInsert += "//\n";
  809.     $creationExpressionToInsert += "// Copy the spriteIndexPP value into startNumPP so that we can use that\n";
  810.     $creationExpressionToInsert += "// value in the runtime expression.\n";
  811.     $creationExpressionToInsert += "//\n";
  812.     $creationExpressionToInsert += "startIndexPP = spriteNumPP;\n";
  813.     $creationExpressionToInsert += "\n";
  814.     $creationExpressionToInsert += "//\n";
  815.     $creationExpressionToInsert += "// Do not delete the tag below.  It is used to properly\n";
  816.     $creationExpressionToInsert += "// replace this expression if the applySprite() script\n";
  817.     $creationExpressionToInsert += "// is executed again.\n";
  818.     $creationExpressionToInsert += "//\n";
  819.     $creationExpressionToInsert += "// __ApplySprite_End_Tag__\n";
  820.     $creationExpressionToInsert += "\n";
  821.  
  822.     $runtimeExpressionToInsert += "// __ApplySprite_Start_Tag__\n";
  823.     $runtimeExpressionToInsert += "//\n";
  824.     $runtimeExpressionToInsert += "// Do not delete the tag above.  It is used to properly\n";
  825.     $runtimeExpressionToInsert += "// replace this expression if the applySprite() script\n";
  826.     $runtimeExpressionToInsert += "// is executed again.\n";
  827.     $runtimeExpressionToInsert += "//\n";
  828.     $runtimeExpressionToInsert += "int $firstImageNumber = "+$fileTexture+".startCycleExtension;\n";
  829.     $runtimeExpressionToInsert += "int $lastImageNumber = "+$fileTexture+".endCycleExtension;\n";
  830.     $runtimeExpressionToInsert += "int $imageCount = $lastImageNumber - $firstImageNumber + 1;\n";
  831.     $runtimeExpressionToInsert += "int $one_based_start = 1;\n";
  832.     $runtimeExpressionToInsert += "\n";
  833.     $runtimeExpressionToInsert += "if( SpriteAnimation == 1 )\n";
  834.     $runtimeExpressionToInsert += "{\n";
  835.     $runtimeExpressionToInsert += "\t// If particles live forever, the animation option won't work.\n";
  836.     $runtimeExpressionToInsert += "\t// Switch them to constant so that the user can see their animation.\n";
  837.     $runtimeExpressionToInsert += "\t//\n";
  838.     $runtimeExpressionToInsert += ("\tif ("+$particleShape+".lifespanMode == 0) {\n");
  839.     $runtimeExpressionToInsert += ("\t\t"+$particleShape+".lifespanMode = 1;\n");
  840.     $runtimeExpressionToInsert += "\t}\n";
  841.     $runtimeExpressionToInsert += "\n";
  842.     $runtimeExpressionToInsert += "\tif ( SpriteCyclePattern < 4 )\n";
  843.     $runtimeExpressionToInsert += "\t{\n";
  844.     $runtimeExpressionToInsert += "\t\t//\n";
  845.     $runtimeExpressionToInsert += "\t\t// Find out how far into the cycle function to offset the lookup, based\n";
  846.     $runtimeExpressionToInsert += "\t\t// on the startIndexPP of this particle.\n";
  847.     $runtimeExpressionToInsert += "\t\t//\n";
  848.     $runtimeExpressionToInsert += "\t\tfloat $startIndexPPRatio = 0;\n";
  849.     $runtimeExpressionToInsert += "\t\t{\n";
  850.     $runtimeExpressionToInsert += "\t\t\t$startIndexPPRatio = ( startIndexPP - $one_based_start ) / ( $imageCount );\n";
  851.     $runtimeExpressionToInsert += "\n";
  852.     $runtimeExpressionToInsert += "\t\t\t//\n";
  853.     $runtimeExpressionToInsert += "\t\t\t// If the cycle function starts off moving in the negative direction, we move the offset\n";
  854.     $runtimeExpressionToInsert += "\t\t\t// forward a little bit to allow the first value to not immediately change as its value\n";
  855.     $runtimeExpressionToInsert += "\t\t\t// crosses down to the next smaller integer.\n";
  856.     $runtimeExpressionToInsert += "\t\t\t//\n";
  857.     $runtimeExpressionToInsert += "\t\t\tif( SpriteInvertCycle == 1 )\n";
  858.     $runtimeExpressionToInsert += "\t\t\t{\n";
  859.     $runtimeExpressionToInsert += "\t\t\t\t$startIndexPPRatio = ( startIndexPP + 1 - $one_based_start ) / ( $imageCount );\n";
  860.     $runtimeExpressionToInsert += "\t\t\t}\n";
  861.     $runtimeExpressionToInsert += "\n";
  862.     $runtimeExpressionToInsert += "\t\t\t//\n";
  863.     $runtimeExpressionToInsert += "\t\t\t// Adjust the offset into the cycle function so that it starts at the same place\n";
  864.     $runtimeExpressionToInsert += "\t\t\t// as the startIndexPP would be in the function.\n";
  865.     $runtimeExpressionToInsert += "\t\t\t//\n";
  866.     $runtimeExpressionToInsert += "\t\t\tif( SpriteCyclePattern == 0 )\n";
  867.     $runtimeExpressionToInsert += "\t\t\t{\n";
  868.     $runtimeExpressionToInsert += "\t\t\t\t$startIndexPPRatio = $startIndexPPRatio;\n";
  869.     $runtimeExpressionToInsert += "\t\t\t}\n";
  870.     $runtimeExpressionToInsert += "\t\t\telse if( SpriteCyclePattern == 1 )\n";
  871.     $runtimeExpressionToInsert += "\t\t\t{\n";
  872.     $runtimeExpressionToInsert += "\t\t\t\tfloat $ts = ( ( $startIndexPPRatio * 2.0 ) - 1.0 );\n";
  873.     $runtimeExpressionToInsert += "\t\t\t\tfloat $ac = acos( $ts );\n";
  874.     $runtimeExpressionToInsert += "\t\t\t\t$nv = ( 3.14159 - $ac ) / 3.14159;\n";
  875.     $runtimeExpressionToInsert += "\t\t\t\t$startIndexPPRatio = $nv;\n";
  876.     $runtimeExpressionToInsert += "\t\t\t}\n";
  877.     $runtimeExpressionToInsert += "\t\t\telse if( SpriteCyclePattern == 2 )\n";
  878.     $runtimeExpressionToInsert += "\t\t\t{\n";
  879.     $runtimeExpressionToInsert += "\t\t\t\t$startIndexPPRatio = $startIndexPPRatio / 2.0;\n";
  880.     $runtimeExpressionToInsert += "\t\t\t}\n";
  881.     $runtimeExpressionToInsert += "\t\t\telse if( SpriteCyclePattern == 3 )\n";
  882.     $runtimeExpressionToInsert += "\t\t\t{\n";
  883.     $runtimeExpressionToInsert += "\t\t\t\tfloat $ts = ( ( $startIndexPPRatio * 2.0 ) - 1.0 );\n";
  884.     $runtimeExpressionToInsert += "\t\t\t\tfloat $ac = acos( $ts );\n";
  885.     $runtimeExpressionToInsert += "\t\t\t\t$nv = ( 3.14159 - $ac ) / 3.14159;\n";
  886.     $runtimeExpressionToInsert += "\t\t\t\t$startIndexPPRatio = $nv / 2.0;\n";
  887.     $runtimeExpressionToInsert += "\t\t\t}\n";
  888.     $runtimeExpressionToInsert += "\t\t}\n";
  889.     $runtimeExpressionToInsert += "\n";
  890.     $runtimeExpressionToInsert += "\t\t//\n";
  891.     $runtimeExpressionToInsert += "\t\t// Compute the value to use to access the curve function for the given cycle option.\n";
  892.     $runtimeExpressionToInsert += "\t\t//\n";
  893.     $runtimeExpressionToInsert += "\t\tfloat $a = 0;\n";
  894.     $runtimeExpressionToInsert += "\t\t{\n";
  895.     $runtimeExpressionToInsert += "\t\t\tif( SpriteCycleLength <= 0 )\n";
  896.     $runtimeExpressionToInsert += "\t\t\t{\n";
  897.     $runtimeExpressionToInsert += "\t\t\t\tif( SpriteInvertCycle == 0 )\n";
  898.     $runtimeExpressionToInsert += "\t\t\t\t{\n";
  899.     $runtimeExpressionToInsert += "\t\t\t\t\t$a = ( ageNormalized + $startIndexPPRatio ) % 1.0;\n";
  900.     $runtimeExpressionToInsert += "\t\t\t\t}\n";
  901.     $runtimeExpressionToInsert += "\t\t\t\telse\n";
  902.     $runtimeExpressionToInsert += "\t\t\t\t{\n";
  903.     $runtimeExpressionToInsert += "\t\t\t\t\t$a = ( $startIndexPPRatio - ageNormalized );\n";
  904.     $runtimeExpressionToInsert += "\t\t\t\t\twhile( $a < 0 )\n";
  905.     $runtimeExpressionToInsert += "\t\t\t\t\t{\n";
  906.     $runtimeExpressionToInsert += "\t\t\t\t\t\t$a = $a + 1.0;\n";
  907.     $runtimeExpressionToInsert += "\t\t\t\t\t}\n";
  908.     $runtimeExpressionToInsert += "\t\t\t\t}\n";
  909.     $runtimeExpressionToInsert += "\t\t\t}\n";
  910.     $runtimeExpressionToInsert += "\t\t\telse\n";
  911.     $runtimeExpressionToInsert += "\t\t\t{\n";
  912.     $runtimeExpressionToInsert += "\t\t\t\tfloat $fps = 30.0;\n";
  913.     $runtimeExpressionToInsert += "\t\t\t\tstring $timeUnit = `currentUnit -q -time`;\n";
  914.     $runtimeExpressionToInsert += "\t\t\t\tif( $timeUnit == \"film\")\n";
  915.     $runtimeExpressionToInsert += "\t\t\t\t\t$fps = 24.0;\n";
  916.     $runtimeExpressionToInsert += "\t\t\t\telse if( $timeUnit == \"ntsc\")\n";
  917.     $runtimeExpressionToInsert += "\t\t\t\t\t$fps = 30.0;\n";
  918.     $runtimeExpressionToInsert += "\t\t\t\telse if( $timeUnit == \"pal\")\n";
  919.     $runtimeExpressionToInsert += "\t\t\t\t\t$fps = 25.0;\n";
  920.     $runtimeExpressionToInsert += "\t\t\t\telse if( $timeUnit == \"game\")\n";
  921.     $runtimeExpressionToInsert += "\t\t\t\t\t$fps = 15.0;\n";
  922.     $runtimeExpressionToInsert += "\t\t\t\telse if( $timeUnit == \"palf\")\n";
  923.     $runtimeExpressionToInsert += "\t\t\t\t\t$fps = 50.0;\n";
  924.     $runtimeExpressionToInsert += "\t\t\t\telse if( $timeUnit == \"ntscf\")\n";
  925.     $runtimeExpressionToInsert += "\t\t\t\t\t$fsp = 60.0;\n";
  926.     $runtimeExpressionToInsert += "\n";
  927.     $runtimeExpressionToInsert += "\t\t\t\tfloat $age = age;\n";
  928.     $runtimeExpressionToInsert += "\t\t\t\tfloat $ageFrames = $age * $fps;\n";
  929.     $runtimeExpressionToInsert += "\t\t\t\tif( SpriteInvertCycle == 0 )\n";
  930.     $runtimeExpressionToInsert += "\t\t\t\t{\n";
  931.     $runtimeExpressionToInsert += "\t\t\t\t\t$a = ( ( $ageFrames / SpriteCycleLength ) + $startIndexPPRatio ) % 1.0;\n";
  932.     $runtimeExpressionToInsert += "\t\t\t\t}\n";
  933.     $runtimeExpressionToInsert += "\t\t\t\telse\n";
  934.     $runtimeExpressionToInsert += "\t\t\t\t{\n";
  935.     $runtimeExpressionToInsert += "\t\t\t\t\t$a = ( $startIndexPPRatio - ( $ageFrames / SpriteCycleLength ) );\n";
  936.     $runtimeExpressionToInsert += "\t\t\t\t\twhile( $a < 0 )\n";
  937.     $runtimeExpressionToInsert += "\t\t\t\t\t{\n";
  938.     $runtimeExpressionToInsert += "\t\t\t\t\t\t$a = $a + 1.0;\n";
  939.     $runtimeExpressionToInsert += "\t\t\t\t\t}\n";
  940.     $runtimeExpressionToInsert += "\t\t\t\t}\n";
  941.     $runtimeExpressionToInsert += "\t\t\t}\n";
  942.     $runtimeExpressionToInsert += "\n";
  943.     $runtimeExpressionToInsert += "\t\t\tif( ( SpriteCyclePattern == 2 ) ||\n";
  944.     $runtimeExpressionToInsert += "\t\t\t    ( SpriteCyclePattern == 3 ) )\n";
  945.     $runtimeExpressionToInsert += "\t\t\t{\n";
  946.     $runtimeExpressionToInsert += "\t\t\t\t$a = $a * 2.0;\n";
  947.     $runtimeExpressionToInsert += "\t\t\t}\n";
  948.     $runtimeExpressionToInsert += "\t\t}\n";
  949.     $runtimeExpressionToInsert += "\n";
  950.     $runtimeExpressionToInsert += "\t\t//\n";
  951.     $runtimeExpressionToInsert += "\t\t// Compute value from $a and the curve representing the desired cycle option.\n";
  952.     $runtimeExpressionToInsert += "\t\t//\n";
  953.     $runtimeExpressionToInsert += "\t\tfloat $v = 0;\n";
  954.     $runtimeExpressionToInsert += "\t\t{\n";
  955.     $runtimeExpressionToInsert += "\t\t\t//\n";
  956.     $runtimeExpressionToInsert += "\t\t\t// Linearly increasing.\n";
  957.     $runtimeExpressionToInsert += "\t\t\t//\n";
  958.     $runtimeExpressionToInsert += "\t\t\tif( SpriteCyclePattern == 0 )\n";
  959.     $runtimeExpressionToInsert += "\t\t\t{\n";
  960.     $runtimeExpressionToInsert += "\t\t\t\t$v = 1.0 - abs( 1.0 -$a );\n";
  961.     $runtimeExpressionToInsert += "\t\t\t}\n";
  962.     $runtimeExpressionToInsert += "\t\t\t//\n";
  963.     $runtimeExpressionToInsert += "\t\t\t// Linearly decreasing.\n";
  964.     $runtimeExpressionToInsert += "\t\t\t//\n";
  965.     $runtimeExpressionToInsert += "\t\t\telse if( SpriteCyclePattern == 1 )\n";
  966.     $runtimeExpressionToInsert += "\t\t\t{\n";
  967.     $runtimeExpressionToInsert += "\t\t\t\tfloat $ta = ( $a + 1.0 ) * 3.14159;\n";
  968.     $runtimeExpressionToInsert += "\t\t\t\tfloat $nv = ( cos( $ta ) + 1.0 ) / 2.0;\n";
  969.     $runtimeExpressionToInsert += "\t\t\t\t$v = $nv;\n";
  970.     $runtimeExpressionToInsert += "\t\t\t}\n";
  971.     $runtimeExpressionToInsert += "\t\t\t//\n";
  972.     $runtimeExpressionToInsert += "\t\t\t// Linearly increasing, then decreasing.\n";
  973.     $runtimeExpressionToInsert += "\t\t\t//\n";
  974.     $runtimeExpressionToInsert += "\t\t\telse if( SpriteCyclePattern == 2 )\n";
  975.     $runtimeExpressionToInsert += "\t\t\t{\n";
  976.     $runtimeExpressionToInsert += "\t\t\t\t$v = 1.0 - abs( 1.0 -$a );\n";
  977.     $runtimeExpressionToInsert += "\t\t\t}\n";
  978.     $runtimeExpressionToInsert += "\t\t\t//\n";
  979.     $runtimeExpressionToInsert += "\t\t\t// Linearly decreasing, then increasing.\n";
  980.     $runtimeExpressionToInsert += "\t\t\t//\n";
  981.     $runtimeExpressionToInsert += "\t\t\telse if( SpriteCyclePattern == 3 )\n";
  982.     $runtimeExpressionToInsert += "\t\t\t{\n";
  983.     $runtimeExpressionToInsert += "\t\t\t\tfloat $ta = ( $a + 1.0 ) * 3.14159;\n";
  984.     $runtimeExpressionToInsert += "\t\t\t\tfloat $nv = ( cos( $ta ) + 1.0 ) / 2.0;\n";
  985.     $runtimeExpressionToInsert += "\t\t\t\t$v = $nv;\n";
  986.     $runtimeExpressionToInsert += "\t\t\t}\n";
  987.     $runtimeExpressionToInsert += "\t\t}\n";
  988.     $runtimeExpressionToInsert += "\n";
  989.     $runtimeExpressionToInsert += "\t\t//\n";
  990.     $runtimeExpressionToInsert += "\t\t// Now scale $v by the number of images in the set to\n";
  991.     $runtimeExpressionToInsert += "\t\t// get the index.  Add one to this since the images\n";
  992.     $runtimeExpressionToInsert += "\t\t// for the sprite rendering are considered to be\n";
  993.     $runtimeExpressionToInsert += "\t\t// one-based.  We actually scale by just under the\n";
  994.     $runtimeExpressionToInsert += "\t\t// image count.  Since this is converted to an integer,\n";
  995.     $runtimeExpressionToInsert += "\t\t// this gives the last image equal time in the cycle.\n";
  996.     $runtimeExpressionToInsert += "\t\t//\n";
  997.     $runtimeExpressionToInsert += "\t\tint $finalIndex = $one_based_start + ( $v * ( $imageCount - 0.01 ) );\n";
  998.     $runtimeExpressionToInsert += "\n";
  999.     $runtimeExpressionToInsert += "\t\t//\n";
  1000.     $runtimeExpressionToInsert += "\t\t// Assign the index.\n";
  1001.     $runtimeExpressionToInsert += "\t\t//\n";
  1002.     $runtimeExpressionToInsert += "\t\tspriteNumPP = $finalIndex;\n";
  1003.     $runtimeExpressionToInsert += "\t}\n";
  1004.     $runtimeExpressionToInsert += "     else if( SpriteCyclePattern == 4 )\n";
  1005.     $runtimeExpressionToInsert += "     {\n";
  1006.     $runtimeExpressionToInsert += "         //\n";
  1007.     $runtimeExpressionToInsert += "         // Custom Cycle\n";    
  1008.     $runtimeExpressionToInsert += "         // Put your own expression for spriteNumPP here.\n";
  1009.     $runtimeExpressionToInsert += "         //\n";
  1010.     $runtimeExpressionToInsert += "     }\n";
  1011.     $runtimeExpressionToInsert += "\telse if( SpriteCyclePattern == 5 )\n";
  1012.     $runtimeExpressionToInsert += "\t{\n";
  1013.     $runtimeExpressionToInsert += "\t\tfloat $rampValue = spriteNumRamp;\n";
  1014.     $runtimeExpressionToInsert += "\t\tif( SpriteInvertCycle == 1 )\n";
  1015.     $runtimeExpressionToInsert += "\t\t{\n";
  1016.     $runtimeExpressionToInsert += "\t\t\t$rampValue = 1.0 - $rampValue;\n";
  1017.     $runtimeExpressionToInsert += "\t\t\twhile( $rampValue < 0 )\n";
  1018.     $runtimeExpressionToInsert += "\t\t\t{\n";
  1019.     $runtimeExpressionToInsert += "\t\t\t\t$rampValue = $rampValue + 1.0;\n";
  1020.     $runtimeExpressionToInsert += "\t\t\t}\n";
  1021.     $runtimeExpressionToInsert += "\t\t\tif( $rampValue > 1.0 )\n";
  1022.     $runtimeExpressionToInsert += "\t\t\t{\n";
  1023.     $runtimeExpressionToInsert += "\t\t\t\t$rampValue = $rampValue % 1.0;\n";
  1024.     $runtimeExpressionToInsert += "\t\t\t}\n";
  1025.     $runtimeExpressionToInsert += "\t\t}\n";
  1026.     $runtimeExpressionToInsert += "\t\tint $s = $one_based_start + ( $rampValue * ( $imageCount - 0.01 ) );\n";
  1027.     $runtimeExpressionToInsert += "\t\tif ( SpriteStartOption != 4)\n";
  1028.     $runtimeExpressionToInsert += "\t\t{\n";
  1029.     $runtimeExpressionToInsert += "\t\t\t// offset the particles by the start frame\n";
  1030.     $runtimeExpressionToInsert += "\t\t\t//\n";
  1031.     $runtimeExpressionToInsert += "\t\t\t$s += ( startIndexPP - $one_based_start );\n";
  1032.     $runtimeExpressionToInsert += "\t\t\tif ($s >= $imageCount ) $s -= $imageCount;\n";
  1033.     $runtimeExpressionToInsert += "\t\t}\n";
  1034.     $runtimeExpressionToInsert += "\t\tspriteNumPP = $s;\n";
  1035.     $runtimeExpressionToInsert += "\t}\n";
  1036.     $runtimeExpressionToInsert += "}\n";
  1037.     $runtimeExpressionToInsert += "\n";
  1038.     $runtimeExpressionToInsert += "//\n";
  1039.     $runtimeExpressionToInsert += "// Do not delete the tag below.  It is used to properly\n";
  1040.     $runtimeExpressionToInsert += "// replace this expression if the applySprite() script\n";
  1041.     $runtimeExpressionToInsert += "// is executed again.\n";
  1042.     $runtimeExpressionToInsert += "//\n";
  1043.     $runtimeExpressionToInsert += "// __ApplySprite_End_Tag__\n";
  1044.  
  1045.     string $tagMatch =
  1046.         "// __ApplySprite_Start_Tag__.*__ApplySprite_End_Tag__";
  1047.  
  1048.     if( `match $tagMatch $creationExpression` != "" )
  1049.     {
  1050.         $creationExpression = `substitute
  1051.             $tagMatch
  1052.             $creationExpression
  1053.             $creationExpressionToInsert`;
  1054.     }
  1055.     else
  1056.     {
  1057.         int $ces = size( $creationExpression );
  1058.         if( $ces > 0 && `substring $creationExpression $ces $ces` != "\n" )
  1059.         {
  1060.             $creationExpression += "\n";
  1061.         }
  1062.  
  1063.         $creationExpression += $creationExpressionToInsert;
  1064.     }
  1065.  
  1066.     if( `match $tagMatch $runtimeExpression` != "" )
  1067.     {
  1068.         $runtimeExpression = `substitute
  1069.             $tagMatch
  1070.             $runtimeExpression
  1071.             $runtimeExpressionToInsert`;
  1072.     }
  1073.     else
  1074.     {
  1075.         int $res = size( $runtimeExpression );
  1076.         if( $res > 0 && `substring $runtimeExpression $res $res` != "\n" )
  1077.         {
  1078.             $runtimeExpression += "\n";
  1079.         }
  1080.  
  1081.         $runtimeExpression += $runtimeExpressionToInsert;
  1082.     }
  1083.  
  1084.     string $returnValue[] =
  1085.     {
  1086.         $runtimeExpression,
  1087.         $creationExpression
  1088.     };
  1089.  
  1090.     return $returnValue;
  1091. }
  1092.  
  1093. global proc addSpriteHelpScriptNode()
  1094. {
  1095.     string $scriptNodes[] = `ls -type script`;
  1096.     int $foundNode = 0;
  1097.     int $s;
  1098.     for( $s = 0; $s < size( $scriptNodes ); $s ++ )
  1099.     {
  1100.         if( `attributeQuery -exists -node $scriptNodes[$s] "__spriteHelpNode__"` == 1 )
  1101.         {
  1102.             $foundNode = 1;
  1103.         }
  1104.     }
  1105.  
  1106.     if( $foundNode == 0 )
  1107.     {
  1108.         string $script = "";
  1109.         $script += "//\n";
  1110.         $script += "// This node initializes the sprite help window\n";
  1111.         $script += "// when the file is opened.\n";
  1112.         $script += "//\n";
  1113.         $script += "string $spriteParticles[];\n";
  1114.         $script += "clear( $spriteParticles );\n";
  1115.         $script += "string $particles[] = `ls -type particle`;\n";
  1116.         $script += "int $p;\n";
  1117.         $script += "for( $p = 0; $p < size( $particles ); $p ++ )\n";
  1118.         $script += "{\n";
  1119.         $script += "    if( `attributeQuery -exists -node $particles[$p] \"ShowSpriteHelp\"` == 1 )\n";
  1120.         $script += "    {\n";
  1121.         $script += "        // scriptJob -attributeChange ($particles[$p]+\".ShowSpriteHelp\") \"showSpriteHelp()\";\n";
  1122.         $script += "    }\n";
  1123.         $script += "}\n";
  1124.         $script += "\n";
  1125.  
  1126.         string $scriptNode = `scriptNode -beforeScript $script -scriptType 1 -name "SpriteHelpScript"`;
  1127.         addAttr -at message -ln "__spriteHelpNode__" $scriptNode;
  1128.     }
  1129. }
  1130.